from digits import digits
from primes import prime, PrimeChecker

def createReplacementPattern(n):
    lastDigit = n % 10
    digitList = [str(d) for d in digits(n) if d != lastDigit]
    
    s = str(n)
    return [s.replace(d, "x") for d in digitList]

def fillinReplacementPattern(p):
    return [int(p.replace("x", str(d))) for d in xrange(0, 10)]

def solution1():
    checker = PrimeChecker(1000000)
    generator = prime()
    
    for p in generator:
        if p > 50000: break
        
    print "go!"
    
    for p in generator:                
        s = str(p)       
        for c in '012':
            if s.count(c) == 0: continue
            
            found = 0
            
            for d in '0123456789':
                r = int(s.replace(c, d))                
                if r >= p and checker.is_prime(r): found += 1
            
            if (8 <= found):
                print p
                exit()
            
def solution2():
    checker = PrimeChecker(1000000)
    generator = prime()

    for p in generator: 
        if p < 10000: continue
         
        s = str(p) 
        for c in '012': 
            if s.count(c) not in (3,6,9): continue
             
            losers = [] 
            winners = [] 
            for d in '0123456789': 
                n = int(s.replace(c,d)) 
                # see if candidate family member is prime 
                if d >= c and checker.is_prime(n): 
                    winners.append(n) 
                else: 
                    losers.append(n) 

                # if more than 2 losers, then can't be a 
                # family of eight primes 
                if len(losers) > 2: break 

                return winners
            
print solution1()
